package com.smartcommunity.elasticsearch.house;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.smartcommunity.es.common.EsBasedataEnum;
import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.smartcommunity.bean.constant.Constant;
import com.smartcommunity.elasticsearch.base.impl.BasedataDataService;
import com.smartcommunity.entity.village.EsHouseEntity;
import com.smartcommunity.entity.village.House;
import com.smartcommunity.exception.BusinessException;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName 房屋ES操作API
 * @Author yangyi
 * @Description TODO
 * @Date 2020/6/20 14:01
 * @Version 1.0
 */

@Component
@Slf4j
public class HouseEsApi {

	@Autowired
	private BasedataDataService basedataDataService;
	
	/**
	 * 新增或修改房屋入库
	 * @param house
	 * @throws BusinessException
	 */
	public void saveOrUpdate(House house) throws BusinessException {
		if(house!=null) {
			EsHouseEntity esHouse = EsHouseEntity.parseFrom(house);
	        this.saveOrUpdate(esHouse);
		}
	}
	
	/**
	 * 新增或修改房屋入库
	 * @param esHouse
	 * @throws BusinessException
	 */
	private void saveOrUpdate(EsHouseEntity esHouse) throws BusinessException {
		if(esHouse!=null) {
	        Map<String, String> houseDataMap = new HashMap<String, String>();
	        houseDataMap.put(esHouse.getId()+Constant.SEPARATOR+EsBasedataEnum.HOUSE.getIntValue(), esHouse.toString());
	        basedataDataService.batchReplace(basedataDataService.getIndexName(null), houseDataMap);
		}
	}
	
	/**
	 * 批量新增或修改房屋入库
	 * @param houseList
	 * @throws BusinessException
	 */
	public void batchSaveOrUpdateHouse(List<House> houseList) throws BusinessException {
		if(CollectionUtils.isNotEmpty(houseList)) {
			List<EsHouseEntity> esHouseList = new ArrayList<EsHouseEntity>();
			houseList.forEach(house->{
				EsHouseEntity esHouse  = EsHouseEntity.parseFrom(house);
		        esHouseList.add(esHouse);
			});
			this.batchSaveOrUpdateEsHouse(esHouseList);
		}
	}
	
	/**
	 * 批量新增或修改房屋入库
	 * @param esHouseList
	 * @throws BusinessException
	 */
	private void batchSaveOrUpdateEsHouse(List<EsHouseEntity> esHouseList) throws BusinessException {
		if(CollectionUtils.isNotEmpty(esHouseList)) {
			Map<String, String> houseDataMap = new HashMap<String, String>();
			esHouseList.forEach(esHouse->{
		        houseDataMap.put(esHouse.getId()+Constant.SEPARATOR+EsBasedataEnum.HOUSE.getIntValue(), esHouse.toString());
			});
	        basedataDataService.batchReplace(basedataDataService.getIndexName(null), houseDataMap);
		}
	}
	
	/**
	 * 修改房屋部分属性
	 * @param id
	 * @param valueMap
	 * @throws BusinessException
	 */
	public void update(String id, Map<String, Object> valueMap) throws BusinessException {
		basedataDataService.update(basedataDataService.getIndexName(null), id+Constant.SEPARATOR+EsBasedataEnum.HOUSE.getIntValue(), valueMap);
	}
	
	/**
	 * 批量修改房屋部分属性
	 * @param map
	 * @throws BusinessException
	 */
	public void batchUpdate(Map<String, Map<String, Object>> map) throws BusinessException {
		basedataDataService.batchUpdate(basedataDataService.getIndexName(null), map);
	}
	
	/**
	 * 删除房屋
	 * @param id
	 * @throws BusinessException
	 */
	public void delete(String id) throws BusinessException {
		basedataDataService.deleteAndRefresh(basedataDataService.getIndexName(null), id+Constant.SEPARATOR+EsBasedataEnum.HOUSE.getIntValue());
	}
	
	/**
	 * 批量删除房屋
	 * @param idList
	 * @throws BusinessException
	 */
	public void batchDelete(List<String> idList) throws BusinessException {
		if(CollectionUtils.isNotEmpty(idList)) {
			idList.forEach(id->{
				this.delete(id);
			});
		}
	}
	
	/**
	 * 根据条件删除房屋
	 * @param queryBuilder
	 * @throws BusinessException
	 */
	public void deleteQuery(QueryBuilder queryBuilder) throws BusinessException {
		basedataDataService.deleteQuery(basedataDataService.getIndexName(null), queryBuilder);
	}	
	
	/**
	 * 根据id查询房屋
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	public EsHouseEntity getDataById(String id) throws BusinessException {
		String[] indexName = new String[] {basedataDataService.getIndexName(null)};
		List<Map<String, Object>> dataList = basedataDataService.getDataById(indexName, id+Constant.SEPARATOR+EsBasedataEnum.HOUSE.getIntValue());
		if(CollectionUtils.isNotEmpty(dataList)) {
			return JSON.parseObject(JSON.toJSONString(dataList.get(0)), EsHouseEntity.class);
		}
		return null;
	}
	
}
